package com.thinkfit.jy.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.blankj.utilcode.util.ToastUtils;
import com.common.lib.bean.ApplicationBean;
import com.common.lib.utils.LogUtil;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DeviceTypeUtils {

    public static final String TENGXUN = "com.tencent.qqlive";
    public static final String CNTV = "cn.cntv";
    public static final String CNTVHD = "cn.cntvhd";
    public static final String CCTV = "com.cctv.yangshipin.app.androidp";
    public static final String LEBO = "com.hpplay.happyplay.aw";
    public static final String XINGHUO = "com.aesq.zb";
    public static final String YINGSHICANG = "com.mygithub0.tvbox0.osdX";

    /**
     * 跑步机
     */
    public static final String TREADMILL = "0";
    /**
     * 椭圆机
     */
    public static final String ELLIPTICAL_MACHINE = "10";
    /**
     * 动感单车
     */
    public static final String SPINNING = "20";
    /**
     * 划船机
     */
    public static final String ROWING_MACHINE = "30";
    /**
     * 走步机
     */
    public static final String WALKING_TREADMILL = "50";
    /**
     * 踏步机
     */
    public static final String MARKTIME_MACHINE = "D1";
    /**
     * 跳绳
     */
    public static final String ROPE_SKIPPING = "F0";
    /**
     * 健腹轮
     */
    public static final String POWER_ROLLER = "F1";
    /**
     * 筋膜枪
     */
    public static final String MASSAGE_GUN = "F2";
    /**
     * 俯卧撑架
     */
    public static final String PUSH_UP_BAR = "F3";
    /**
     * 哑铃
     */
    public static final String DUMBBELL = "F4";
    /**
     * 拉力器
     */
    public static final String CHEST_DEVELOPER = "F5";
    /**
     * 塑身板
     */
    public static final String BODY_SCULPTING_BOARD = "F6";
    /**
     * 力量器械
     */
    public static final String STRENGTH_APPARATUS = "F8";
    /**
     * 按摩枕
     */
    public static final String MASSAGE_PILLOW = "FA";
    /**
     * 骑行台
     */
    public static final String BIKE_TRAINER = "FB";

    private static DeviceTypeUtils deviceTypeUtils;

    public static DeviceTypeUtils getInstance(){
        if(deviceTypeUtils == null){
            deviceTypeUtils = new DeviceTypeUtils();
        }
        return deviceTypeUtils;
    }


    /**
     * 连接设备判断  0:蓝牙  1:串口
     * @return
     */
    public static int connectDeviceType(){
        if(BluetoothOrder.getInstance().isConnect()){
            return 0;
        }
        if(SerialPortManager.getInstance().isConnected()) {
            return 1;
        }
        return 0;
    }

    /**
     * 是否有连接蓝牙或者串口
     * @return
     */
    public static boolean isConnect(){
        if(SerialPortManager.getInstance().isConnected()||BluetoothOrder.getInstance().isConnect()){
            return true;
        }
        return false;
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager)context.getSystemService("activity");
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        Iterator var3 = appProcesses.iterator();

        ActivityManager.RunningAppProcessInfo appProcess;
        do {
            if (!var3.hasNext()) {
                return false;
            }

            appProcess = (ActivityManager.RunningAppProcessInfo)var3.next();
        } while(!appProcess.processName.equals(context.getPackageName()));

        if (appProcess.importance == 100) {
            System.out.println("ddddddddddddddddddddddd  前台"+new Object[]{appProcess.processName});
            return false;
        } else {
            System.out.println("ddddddddddddddddddddddd  后台"+new Object[]{appProcess.processName});
            return true;
        }
    }

    public static void showBackButton(Context context){
        if(DeviceTypeUtils.isBackground(context)){
//            BackButtonViewUtils.getInstance().initView(context);
//            SportsStatusBarViewUtils.getInstance().initView(context);
//            SportsStatusBarViewUtils.getInstance().atThirdParty(true);
//            ShowSpeedViewUtils.getInstance().initView(context);
        }
    }

    /**
     * 获取设备类型
     * @return 1动感单车 2跑步机 3.走步机 5划船器 6椭圆机 10跳绳等
     */
    public static int getDeviceType(){
        if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)){
            return 1;
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
            return 2;
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.WALKING_TREADMILL)){
            return 3;
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ELLIPTICAL_MACHINE)){
            return 6;
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)){
            return 5;
        }

        return 2;
    }

    /**
     * 是否是跑步机
     * @return
     */
    public boolean isTreadmill(){
        if(getDeviceType() == 2){
            return true;
        }
        return false;
    }

    /**
     * 通过包名打开第三方应用
     * @param context
     * @param packageName
     */
    public void openApp(Context context,String packageName){
        try {
            if(isInstallApp(context,packageName)){
//                System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    open : "+downloadData.packageName);
//                        SharedPreferencesUtils.getInstance(ApplicationActivity.this).putString(Constant.CURRENT_THIRDAPP_PACKGAME,downloadData.packageName);
                RunningAppUtils.getInstance().init(context);
                ArrayList<ApplicationBean> currentRunningApp = RunningAppUtils.getInstance().getCurrentRunningApp();
                boolean isOpen = RunningAppUtils.getInstance().isOpen(packageName);
                LogUtils.getInstance().l(LogUtils.HINT,"aaaaaaaaaaaaaaaaaaaaaaaaa   isOpen : "+isOpen);
                LogUtils.getInstance().l(LogUtils.HINT,"aaaaaaaaaaaaaaaaaaaaaaaaa   currentRunningApp.size() : "+currentRunningApp.size());
                if(currentRunningApp.size()>=3 && !isOpen){
                    DialogCloseApkViewUtils.getInstance().initView(context,packageName);
                }else {
                    PackageManager packageManager = context.getPackageManager();
                    Intent intent = packageManager.getLaunchIntentForPackage(packageName);
                    if(intent != null){
                        context.startActivity(intent);
                    }else {
                        LogUtil.LogE("打开失败");
                    }
                }

            }else {
                ToastUtils.showShort(context.getString(R.string.app_toast_hint_3));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean isInstallApp(Context context,String packageName){
        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
        for (PackageInfo packageInfo:installedPackages){
            System.out.println("ddddddddddddddddddddddd  isInstallApp  packageInfo : "+packageInfo.packageName);
            if(packageInfo.packageName.equals(packageName)){
                return true;
            }
        }
        return false;
    }

    public void killApp(Context context,String packageName){
        try {
            ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
            Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
            forceStopPackage.setAccessible(true);
            forceStopPackage.invoke(am, packageName);
            System.out.println("TimerV force stop package "+packageName+" successful");
        }catch(Exception ex) {
            ex.printStackTrace();
            System.err.println("TimerV force stop package "+packageName+" error!");
        }

    }


}
